package com.bruce.shop.filter;

import com.bruce.shop.commons.utils.StaticResources;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author BruceXia
 * @version 1.0.0
 * @description:后台登录过滤器
 * @date 2022/9/199:23
 */
@WebFilter("/UserController/*,/ProductTypeController/*,/back/*"

)
public class Filter02_BackLogin implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {

        // 1.类型转换
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;

        System.out.println(req.getRequestURI());

        // 2.判断当前用户是否登录
        Object sysUser = req.getSession().getAttribute("sysUser");

        if (StaticResources.isStaticResources(req.getRequestURI()) || sysUser != null) {
            // 已经登录了，放行
            chain.doFilter(req, resp);
        } else {
            resp.sendRedirect(req.getContextPath() + "/backLogin.jsp");
        }
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
